package app.ui;

import java.awt.Color;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import app.action.WorkScrAct;

public class WorkScr extends AbsCommonPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -503580731399061788L;

	protected String[] strLabels = { "Merge", "Amount of file: ", "File name #:" };
	protected JLabel jLabel; // amount of file
	protected JLabel jLabel2; // ODI label
	protected JButton jButton; // merge
	protected JTextField jTextField; // ODI
	protected JProgressBar jProgressBar; // progress

	protected JTable jTable; // listing files
	protected TableModel tableModel;
	protected JScrollPane jScrollPane; // hold the table of files

	protected String[] listFileName;
	protected int amtOfFile;
	protected WorkScrAct workScrAct;

	public WorkScr(MainFrame mFrame) {
		super(mFrame);

		this.workScrAct = new WorkScrAct(this);
	}

	@Override
	protected void addComponents() {
		// Construction
		this.listFileName = mFrame.getAppUtilities().getFileName();
		this.amtOfFile = listFileName.length;

		this.jLabel = new JLabel(getStrLabels()[1] + amtOfFile);
		this.jLabel2 = new JLabel(getStrLabels()[2]);
		this.jButton = new JButton(getStrLabels()[0]);
		this.jTextField = new JTextField();
		this.jProgressBar = new JProgressBar(0, this.amtOfFile);

		Object[] rowName = { "File Names" };
		this.tableModel = new DefaultTableModel(rowName, amtOfFile);
		this.jTable = new JTable(tableModel);
		this.jScrollPane = new JScrollPane(jTable);

		// add to this panel
		add(jLabel);
		add(jLabel2);
		add(jButton);
		add(jTextField);
		add(jScrollPane);
		add(jProgressBar);
	}

	@Override
	protected void initializeComponents() {
		// jScrollPane
		jScrollPane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		// tableModel
		for(int i = 0; i < amtOfFile; i++) {
			tableModel.setValueAt(listFileName[i], i, 0);
		}
		// jTable
		jTable.setRowHeight(20);
		// jButton
		jButton.addActionListener(workScrAct);
		// jTextField
		jTextField.setText(mFrame.getAppUtilities().getLastFileName());
	}

	@Override
	protected void resizeComponents() {
		final int X = (int) (getWidth() - 1.2 * btnWid);
		int width = btnWid - 8;
		int height = btnHei;
		int x = X + 4;
		int y = getHeight() / 2;

		// jLabel
		jLabel.setBounds(x, y, width, height);
		// jLabel2
		y += 3 * height;
		jLabel2.setBounds(x, y, width, height);
		// jTextField
		y += height;
		jTextField.setBounds(x, y, width, height);
		// jButton
		y += 2 * height;
		width = btnWid;
		jButton.setBounds(x, y, width, height);
		// jProgressBar
		width = this.getWidth() - x + 3;
		x = X + 1;
		height = btnHei / 2;
		y = this.getHeight() - height;
		jProgressBar.setBounds(x, y, width, height);

		// jScrollPane
		x = 0;
		y = 0;
		width = X;
		height = getHeight();
		jScrollPane.setBounds(x, y, width, height);
		jScrollPane.updateUI();
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String[] getStrLabels() {
		return strLabels;
	}

	public JLabel getjLabel() {
		return jLabel;
	}

	public JLabel getjLabel2() {
		return jLabel2;
	}

	public JButton getjButton() {
		return jButton;
	}

	public JTextField getjTextField() {
		return jTextField;
	}

	public void doMerging() {
		String fileName = jTextField.getText();
		if(fileName.length() < 1) {
			fileName = "OutputFile.txt";
		}
		else {
			fileName = fileName.concat(".txt");
		}

		mFrame.getAppUtilities().doMerging(fileName, jProgressBar);

		finishWork();
	}

	private void finishWork() {
		File workingDir = null;
		workingDir = new File(mFrame.getAppUtilities().getLastLocation());

		try {
			Desktop.getDesktop().open(workingDir);
		}catch (IOException e) {
			e.printStackTrace();
		}

		mFrame.closing();
	}

}
